// Copyrights 2021 WU PENG. All rights reserved.
//

#ifndef MAX7219_H
#define MAX7219_H

#include "Arduino.h"

// Decode-Mode
const uint8_t DecodeModeNone = 0x00;
// Scan-Limit
const uint8_t ScanLimitAll = 0x07;
// Intensity
const uint8_t IntensityMin = 0;
const uint8_t IntensityMax = 15;
// Shutdown
const uint8_t PowerOff = 0;
const uint8_t PowerOn  = 1;
// Display-Test
const uint8_t DisplayNormal = 0;
const uint8_t DisplayTest   = 1;


class Max7219 {
private:
  int dinPin;
  int clkPin;
  int csPin;

  uint8_t blockNumber;
  uint8_t *buffer;

public:
  Max7219(int dinPin, int csPin, int clkPin, uint8_t blockNumber);
  ~Max7219();

  void begin();

  void SetNoOp();
  void SetDecodeMode(uint8_t value);
  void SetIntensity(uint8_t value);
  void SetScanLimit(uint8_t value);
  void SetPower(uint8_t value);
  void SetDisplay(uint8_t value);
  
  void ClearBuffer();
  int  SetBuffer(uint8_t block, uint8_t addr, uint8_t value);
  void DrawText(uint8_t pos, char c);
  void Flush();

private:
  void write(uint8_t addr, uint8_t value);
  void setRegister(uint8_t reg, uint8_t value);

};

#endif
